import { Module } from 'vuex'
import { minCache } from '@/utils/min-cache'
import { getInfo, logout } from '@/api/modules/login'
import getDeviceInfo from '@/utils/get-device-info'
import { router } from '@/router'
interface loginAPiInfoParams {
  inviteUserId?: number | string //邀请人id
  deviceInfo?: string //登陆设备
  merchantId?: number | string //商家id
  storeId?: number | string //店铺id
  agentUserId?: number | string //分销员id
}
interface IBadgeCount {
  [key: string]: number
}
interface State {
  unionLoginId: number | string
  loginAPiInfo: loginAPiInfoParams //登录接口所用到的全局基本数据
  userInfo: any //用户基本数据
  ut: string //用户唯一标识
  isNew: boolean //是否是新用户
  mobile: string // 用户手机号
  oneClickLogin: boolean //是否支持一键登录
  badgeCount: IBadgeCount
  aggrement: boolean //是否同意协议
}

const store: Module<State, unknown> = {
  namespaced: true,
  state() {
    return {
      unionLoginId: minCache.get('unionLoginId'),
      ut: minCache.get('ut'),
      isNew: true,
      oneClickLogin: true,
      mobile: '',
      loginAPiInfo: {
        inviteUserId: '',
        deviceInfo: getDeviceInfo(),
        merchantId: '',
        storeId: '',
        agentUserId: '',
      },
      userInfo: {} as any,
      aggrement: false,
      badgeCount: {}, // 订单信息
    }
  },
  getters: {
    getUserInfo(state) {
      return state.userInfo
    },
    getLoginAPiInfo(state) {
      return state.loginAPiInfo || minCache.get('loginAPiInfo')
    },
  },
  mutations: {
    setUnionLoginId(state, value) {
      state.unionLoginId = value

      if (value) {
        minCache.set('unionLoginId', value)
      } else {
        minCache.delete('unionLoginId')
      }
    },
    setUt(state, value) {
      minCache.set('ut', value)
      state.ut = value
    },
    setIsNew(state, value) {
      state.isNew = value
    },
    setLoginAPiInfo(state, value) {
      state.loginAPiInfo = { ...state.loginAPiInfo, ...value }
      minCache.set('loginAPiInfo', state.loginAPiInfo)
    },
    setUserInfo(state, value) {
      state.userInfo = value
    },
    setMobile(state, value) {
      minCache.set('mobile', value)
      state.mobile = value
    },
    setOneClickLogin(state, value) {
      state.oneClickLogin = value
    },
    setBadgeCount(state, data) {
      state.badgeCount = data
    },
    setLoginAgreement(state, data) {
      state.aggrement = data
    },
  },

  actions: {
    async logout({ commit }) {
      await logout()
      minCache.clear()
      commit('setUserInfo', {})
    },
    async getUserInfo({ commit }) {
      if (minCache.get('ut')) {
        try {
          const { user, roles }: any = await getInfo()
          if (user === undefined && roles === undefined) {
            throw new Error('获取用户信息失败！')
          } else {
            const data = { ...user, roles }
            commit('setUserInfo', data)
            return data
          }
        } catch (e) {
          minCache.clear()
          commit('setUserInfo', {})

          router.replaceAll({
            name: 'UserLogin',
          })
          return
        }
      }
    },
  },
}

export default store
